package com.ruoyi.admin.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.admin.domain.SystemUserLogin;
import com.ruoyi.admin.mapper.SystemUserLoginMapper;
import com.ruoyi.admin.service.ISystemUserLoginService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SystemUserLoginServiceImpl implements ISystemUserLoginService {

    private final SystemUserLoginMapper systemUserLoginMapper;

    @Override
    public void inertLogin(String username, String token, HttpServletRequest httpServletRequest, String userCode) {
        String ipAddr = IpUtils.getIpAddr(httpServletRequest);
        SystemUserLogin systemUserLogin = new SystemUserLogin();
        Snowflake snowflake = IdUtil.getSnowflake();
        systemUserLogin.setId(snowflake.nextId());
        systemUserLogin.setLoginUser(username);
        systemUserLogin.setLoginTime(DateUtils.getTime());
        systemUserLogin.setLoginIp(ipAddr);
        systemUserLogin.setToken(token);
        systemUserLogin.setUserCode(userCode);
        systemUserLoginMapper.insert(systemUserLogin);
    }

    @Override
    public List<SystemUserLogin> getAllLogin() {
        QueryWrapper<SystemUserLogin> objectQueryWrapper = new QueryWrapper<>();
        List<SystemUserLogin> systemUserLogins = systemUserLoginMapper.selectList(objectQueryWrapper);
        return systemUserLogins;
    }

    @Override
    public List<SystemUserLogin> getAll(SystemUserLogin reqVO) {
        List<SystemUserLogin> systemUserLogins = systemUserLoginMapper.select(reqVO);
        return systemUserLogins;
    }
}
